package com.cndw.kungfu.business;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.MD5;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessPayQQ {

	protected static final String LINK = "&";

	protected static final String EQUAL = "=";

	protected static final String key = "key";

	protected static final String sign = "sign";

	private boolean isSignOk(TreeMap<String, String> sendMap) {
		String signData = sendMap.remove(sign);
		StringBuilder sb = new StringBuilder();
		for (Entry<String, String> entry : sendMap.entrySet()) {
			sb.append(entry.getKey()).append(EQUAL);
			sb.append(entry.getValue()).append(LINK);
		}
		sb.append(key).append(EQUAL).append(ConstantsConfig.getSysCgKeyPay());
		boolean isOk = signData.equalsIgnoreCase(MD5.compile(sb.toString()));
		return isOk;
	}

	@UnAuth
	@GmCommand
	public ResponseMessage qqCheck(Session session, String gmsign, Map<String, String> dataMap) {
		TreeMap<String, String> sendMap = new TreeMap<String, String>(dataMap);
		if (!isSignOk(sendMap)) {
			LogUtil.getMain().error("err sign ");
			return ResponseMessage.getFailMessage(-1002);
		}
		String openid = sendMap.get("openid").trim();
		int app_custom = Integer.valueOf(sendMap.get("app_custom").trim());

		long playerId = Context.getPlayerServer().getPlayerIdByAccount(openid);
		if (playerId == 0) {
			return ResponseMessage.getFailMessage(-1008);
		}
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player == null) {
			return ResponseMessage.getFailMessage(-1008);
		}
		int jxe = player.getDataInfo().intDataGet(ConstantsIntKey.jxe);
		boolean ok = jxe == app_custom;
		LogUtil.getSystem().debug(openid + " app_custom " + app_custom + " jxe " + jxe);
		return ResponseMessage.getSuccMessage(ok ? 1 : -1100);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage qqAward(Session session, String gmsign, Map<String, String> dataMap) {
		TreeMap<String, String> sendMap = new TreeMap<String, String>(dataMap);
		if (!isSignOk(sendMap)) {
			LogUtil.getMain().error("err sign ");
			return ResponseMessage.getFailMessage(-1002);
		}
		String openid = sendMap.get("openid").trim();
		String payitem = sendMap.get("payitem").trim();
		String contractid = sendMap.get("contractid").trim();
		String billno = sendMap.get("billno").trim();
		String providetype = sendMap.get("providetype").trim();
		String serverId = sendMap.get("serverId").trim();
		String[] mes = payitem.split("\\*");
		int goodsId = 0,num =0;
		try {
			goodsId = Integer.valueOf(mes[0]);
			num = Integer.valueOf(mes[1]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!serverId.equalsIgnoreCase(ConstantsConfig.getSysCgSvrId())) {
			LogUtil.getSystem().error(" err " + serverId);
		}

		long playerId = Context.getPlayerServer().getPlayerIdByAccount(openid);
		if (playerId == 0) {
			return ResponseMessage.getFailMessage(-1008);
		}
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player == null) {
			return ResponseMessage.getFailMessage(-1008);
		}
		if (player.getDataInfo().intDataGet(ConstantsIntKey.jx) ==0) {
			LogUtil.getSystem().debug(openid + " award is miss ");
			return ResponseMessage.getSuccMessage(1);
		}
		Context.getGoodsServer().giveGoodsBing(playerId, goodsId, num, 1);
		player.getDataInfo().intDataRemove(ConstantsIntKey.jx);
		player.getDataInfo().intDataRemove(ConstantsIntKey.jxe);
		player.getDataInfo().commitSync();

		LogServer.recordLog(player, ConstantsLog.gold, 50008, 1, billno, providetype, payitem, contractid, mes[0]);
		return ResponseMessage.getSuccMessage(1);
	}

}
